package com.yuanduo_app.ui.map.model;

import android.os.Bundle;

import com.google.gson.Gson;
import cn.ydxlm.maplib.entity.Location;
import cn.ydxlm.maplib.entity.Marker;
import com.xujl.fastlib.base.BaseModel;
import com.xujl.fastlib.base.IControl;
import com.xujl.fastlib.http.ApiDisposableObserver;
import com.xujl.fastlib.utils.CollectionsUtils;
import com.xujl.fastlib.utils.NumberUtils;
import com.xujl.fastlib.utils.StringUtils;

import com.yuanduo_app.R;
import com.yuanduo_app.consts.BundleKey;
import com.yuanduo_app.entity.NearbyOilEntity;
import com.yuanduo_app.entity.NearbyOilLevelEntity;
import com.yuanduo_app.http.DataSource;
import com.yuanduo_app.http.RequestCommonParams;
import com.yuanduo_app.utils.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.databinding.ObservableField;


/**
 * <pre>
 *     author : xujl
 *     e-mail : 597355068@qq.com
 *     time   : 2019/08/20
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class NearbyOilActivityModel extends BaseModel {
    public ObservableField<String> currentStationName = new ObservableField<>();
    public ObservableField<String> currentStationSize = new ObservableField<>();
    public ObservableField<String> currentStationAddress = new ObservableField<>();
    public ObservableField<String> currentOilLevel = new ObservableField<>("96");
    public ObservableField<Boolean> hasData = new ObservableField<>(true);
    private NearbyOilEntity mNearbyOilEntity;
    public Location mLocation = new Location();
    public List<NearbyOilLevelEntity> mOilLevelEntities = new ArrayList<>();
    public List<NearbyOilEntity> mOilEntities = new ArrayList<>();


    @Override
    public void init (IControl control) {

    }


    public void getAllOilType (ApiDisposableObserver<List<NearbyOilLevelEntity>> callback) {
        final RequestCommonParams.Builder builder = RequestCommonParams.createParamBuilder();
        request(DataSource.createApi().findAllOilType(builder.build()), callback);
    }


    public void queryStationInfo (ApiDisposableObserver<List<NearbyOilEntity>> callback) {
        final RequestCommonParams.Builder builder = RequestCommonParams.createParamBuilder();
        builder.addParams("latitude", mLocation.getLatitude())
                .addParams("longitude", mLocation.getLongitude())
                .addParams("distance", 9999999)
                .addParams("businessNo", currentOilLevel.get());
        request(DataSource.createApi().queryStationInfo(builder.build()), callback);
    }

    public NearbyOilEntity getNearbyOilEntity () {
        return mNearbyOilEntity;
    }

    public void setNearbyOilEntity (List<NearbyOilEntity> list) {
        mOilEntities.clear();
        CollectionsUtils.addAll(mOilEntities, list);
        if (!CollectionsUtils.isEmpty(mOilEntities)) {
            setNearbyOilEntity(mOilEntities.get(0));
            hasData.set(true);
        } else {
            hasData.set(false);
        }
        for (NearbyOilLevelEntity entity : mOilLevelEntities) {
            entity.setChecked(StringUtils.equals(entity.getBusinessNo(), currentOilLevel.get()));
        }

    }

    public void setNearbyOilEntity (NearbyOilEntity entity) {
        mNearbyOilEntity = entity;
        currentStationAddress.set(mNearbyOilEntity.getAddress());
        currentStationName.set(mNearbyOilEntity.getStationName());
        currentStationSize.set("距您：" + MapUtils.getDistance(NumberUtils.doubleOf(mNearbyOilEntity.getDistanceUm())));
    }

    public List<Marker> getMarkerList (List<NearbyOilEntity> data) {
        List<Marker> list = new ArrayList<>();
        for (NearbyOilEntity entity : data) {
            list.add(new Marker(NumberUtils.doubleOf(entity.getLatitude()),
                    NumberUtils.doubleOf(entity.getLongitude()), entity == mNearbyOilEntity ? R.drawable.petrol_station_p : R.drawable.petrol_station));
        }
        return list;
    }

    public void saveBundle (Bundle extras) {
        if (extras != null) {
            String json = extras.getString(BundleKey.DATA);
            HashMap<String, String> map = new Gson().fromJson(json, HashMap.class);
            if (CollectionsUtils.isNotEmpty(map)) {
                currentOilLevel.set(map.get("businessNo"));
            }
        }
    }
}
